import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox, Listbox
import subprocess

# 正确的相对路径定义
UPLOAD_PATH = 'demo/GUI/uploadPath'
CHANG_SCRIPT = 'demo/GUI/change.py'
CREWAI_SCRIPT = 'demo/GUI/crewaiWork.py'

# 初始化上传目录
if not os.path.exists(UPLOAD_PATH):
    os.makedirs(UPLOAD_PATH)

# 更新文件列表
def update_file_list():
    file_list.delete(0, tk.END)
    for file_name in os.listdir(UPLOAD_PATH):
        file_list.insert(tk.END, file_name)

# 上传文件
def upload_file():
    file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
    if file_path:
        shutil.copy(file_path, UPLOAD_PATH)
        update_file_list()
        messagebox.showinfo("上传成功", "文件已上传至指定文件夹！")

# 清空文件夹
def clear_files():
    for file_name in os.listdir(UPLOAD_PATH):
        file_path = os.path.join(UPLOAD_PATH, file_name)
        os.remove(file_path)
    update_file_list()
    messagebox.showinfo("清空成功", "所有文件已删除！")

# 运行chang.py
def run_chang():
    try:
        subprocess.run(['python', CHANG_SCRIPT])
        messagebox.showinfo("转换成功", "chang.py 已成功运行！")
    except Exception as e:
        messagebox.showerror("转换错误", str(e))

# 运行crewaiWork.py
def run_crewaiWork():
    try:
        subprocess.run(['python', CREWAI_SCRIPT])
        messagebox.showinfo("生成成功", "crewaiWork.py 已成功运行！")
    except Exception as e:
        messagebox.showerror("生成错误", str(e))

# 打开做题文件
def open_questions():

    questions_path = 'outputs/GUI/choiceQuestions.md'
    try:
        # 在 Windows 上使用默认程序打开文件
        os.startfile(questions_path)
    except AttributeError:
        try:
            # 在 macOS 上使用 open 命令
            subprocess.run(['open', questions_path])
        except:
            try:
                # 在 Linux 上使用 xdg-open 命令
                subprocess.run(['xdg-open', questions_path])
            except:
                messagebox.showerror("打开错误", "无法打开题目文件！")

# 创建主窗口
root = tk.Tk()
root.title("文件管理器")

# 左侧文件列表
frame_left = tk.Frame(root)
frame_left.pack(side=tk.LEFT, fill=tk.Y)

file_list = Listbox(frame_left, width=40)
file_list.pack(fill=tk.BOTH, expand=True)

# 文件上传按钮
upload_button = tk.Button(frame_left, text="上传PDF文件", command=upload_file)
upload_button.pack(fill=tk.X)

# 右侧功能按钮
frame_right = tk.Frame(root)
frame_right.pack(side=tk.RIGHT, fill=tk.Y)

refresh_button = tk.Button(frame_right, text="刷新", command=update_file_list)
refresh_button.pack(fill=tk.X, padx=10, pady=5)

clear_button = tk.Button(frame_right, text="清空", command=clear_files)
clear_button.pack(fill=tk.X, padx=10, pady=5)

convert_button = tk.Button(frame_right, text="转换", command=run_chang)
convert_button.pack(fill=tk.X, padx=10, pady=5)

generate_button = tk.Button(frame_right, text="生成", command=run_crewaiWork)
generate_button.pack(fill=tk.X, padx=10, pady=5)

# 在右侧框架中添加做题按钮（在 generate_button 之后添加）
question_button = tk.Button(frame_right, text="做题", command=open_questions)
question_button.pack(fill=tk.X, padx=10, pady=5)

# 初始化文件列表
update_file_list()

# 运行主循环
root.mainloop()